/**
 * 
 */   
package com.cares.infobird.web;  

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cares.core.base.controller.BaseController;
import com.cares.core.base.exception.ApplyException;
import com.cares.entity.base.DataGridModel;
import com.cares.entity.base.PageModel;
import com.cares.entity.common.Dic;
import com.cares.entity.infobird.CallBusi;
import com.cares.entity.infobird.OrderStat;
import com.cares.entity.infobird.OrderStatParm;
import com.cares.entity.infobird.OrderStatResult;
import com.cares.infobird.service.CallOrderService;

/**   
 * @作者 Stone
 * @创建日期 2014-10-20 下午5:40:48
 * @版本 V1.0
 * @类说明
 */
@Controller
@RequestMapping(value="/infobird/chart")
public class OrderChartController extends BaseController {
	
	@Resource(name="callOrderService")
	private CallOrderService callOrderService;
	
	/**
	 * GET方法，用户列表页面加载
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String init(HttpServletRequest request) {
		//获取一级业务分类
		List<Dic> voiceList = callOrderService.getParentBusiness("voiceBusiness");
		request.setAttribute("voiceList", voiceList);
		return "infobird/chart/OrderProvChart";
	}
	
	/**
	 * 根据参数，对订单进行分省统计
	 */
	@RequestMapping(value="/provStat", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> provStat(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		OrderStatResult result = new OrderStatResult();
		List<String> provList = null;
		List<Integer> numList = null;
		//获取参数
		
		try {
			if(true) {
				List<OrderStat> statList = callOrderService.getBusiProvStat(null);
				if(statList!=null&&statList.size()>0) {
					provList = new ArrayList<String>();
					numList = new ArrayList<Integer>();
					for (OrderStat stat : statList) {
						provList.add(stat.getProvName());
						numList.add(stat.getNum());
					}
					result.setProvList(provList);
					result.setNumList(numList);

					map.put(AJAX_MESSAGE, "true");
					map.put("data", JSON.toJSON(result));
				} else {
					map.put(AJAX_MESSAGE, "false");
					map.put(ERR_MESSAGE, "订单分省统计数量为空");
				}
			}
		} catch (ApplyException e) {
			e.printStackTrace();
			map.put(AJAX_MESSAGE, "false");
			map.put(ERR_MESSAGE, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			map.put(AJAX_MESSAGE, "false");
			map.put(ERR_MESSAGE, "获取客户信息异常");
		}
		return map;
	}
	
	/**
	 * 根据参数查询分省统计信息列表
	 * @param dgm
	 * @param model
	 * @param response
	 */
	@RequestMapping(value="/provStatList", method=RequestMethod.POST)
	public void getProvStatList(DataGridModel dgm, OrderStatParm model, HttpServletResponse response) {
		int pageNo = dgm.getPage();
		int pageSize = dgm.getRows();
		String order = dgm.getOrder();
		String sort = dgm.getSort();
		PageModel<OrderStatParm> pageModel = new PageModel<OrderStatParm>(pageNo, pageSize);
		pageModel.setOrder(order);
		pageModel.setSort(sort);
		pageModel.setCondition(model);
		String result = callOrderService.getProvStatList(pageModel);
		System.out.println(result);
		try {
			this.renderText(response, result);
		} catch (IOException e) {
			
		}
		
	}
	
}
